package com.qyer.search.DeserializerProcessor;

import com.qyer.log.WarningLogger;
import com.qyer.search.filter.AbstractFilter;
import com.qyer.search.model.SearchParameterOrigin;
import com.qyer.search.model.lastminute.SKU;
import com.qyer.search.model.result.Type1SearchPackage;
import com.qyer.search.utils.JsonOperator;
import com.qyer.search.utils.ModelUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
/**
 * Created by tangzheng on 16/5/11.
 */
public class DeserializerProcessor implements Callable<List<Type1SearchPackage>> {

  private SearchParameterOrigin sp;
  private List<String> resultStringList;
  private boolean simple;
  private boolean basic;
  private boolean meta;
  private Map<Integer, Map<Integer, Integer>> placeSearchCensus;
  private Map<Integer, Map<Integer, Integer>> typeSearchCensus;
  private List<AbstractFilter> filters;
  private long submitTime = 0;

  public DeserializerProcessor(SearchParameterOrigin sp,
                               List<String> resultStringList,
                               boolean simple, boolean basic, boolean meta,
                               Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                               Map<Integer,Map<Integer,Integer>> typeSearchCensus,
                               List<AbstractFilter> filters){
    this.sp = sp;
    this.resultStringList = resultStringList;
    this.simple = simple;
    this.basic = basic;
    this.meta = meta;
    this.placeSearchCensus = placeSearchCensus;
    this.typeSearchCensus = typeSearchCensus;
    this.filters = filters;
  }

  public DeserializerProcessor(SearchParameterOrigin sp, List<String> resultStringList,
                               boolean simple, boolean basic, boolean meta,
                               Map<Integer, Map<Integer, Integer>> placeSearchCensus,
                               Map<Integer, Map<Integer, Integer>> typeSearchCensus,
                               List<AbstractFilter> filters, long submitTime) {
    this.sp = sp;
    this.resultStringList = resultStringList;
    this.simple = simple;
    this.basic = basic;
    this.meta = meta;
    this.placeSearchCensus = placeSearchCensus;
    this.typeSearchCensus = typeSearchCensus;
    this.filters = filters;
    this.submitTime = submitTime;
  }

  @Override public List<Type1SearchPackage> call() throws Exception {
    WarningLogger.logWarning("s:" + this.submitTime + ":e:" + System.currentTimeMillis(), sp);
    int size = filters.size();
    if(meta) {
      if(size == 0) {
        return ModelUtils.packageByType1(this.sp, this.resultStringList, this.simple, this.basic,
                                         placeSearchCensus, typeSearchCensus);
      }else {
        return ModelUtils.packageByType1(this.sp, this.resultStringList, this.simple, this.basic, filters,
                                         placeSearchCensus, typeSearchCensus);
      }
    }else {
      if(size == 0){
        return ModelUtils.packageByType1(this.sp, this.resultStringList, this.simple, this.basic);
      }else {
        return ModelUtils.packageByType1(this.sp, this.resultStringList, this.simple, this.basic, filters);
      }
    }
  }
}
